package interviewclassic;

import java.util.*;

/**
 * @author ZhanBo
 * @date 2020/7/6
 */
public class Solution0102 {
    public static void main(String[] args) {
//        String a = "avcdwqd";
//        char[] chars = a.toCharArray();
//        Character [] characters = new Character[chars.length];
//        for (int i = 0 ; i < chars.length ; i++){
//            characters[i] = chars[i];
//        }
//        List<Character> characterList = Arrays.asList(characters);
//
//       for (Character character : characterList){
//           System.out.print(character);
//       }
//        System.out.println();
//        Collections.sort(characterList);
//
//        for (Character character : characterList){
//            System.out.print(character);
//        }
        System.out.println(CheckPermutation("abcsasd", "sasdbac"));
    }

    /**
     * 面试题 01.02. 判定是否互为字符重排
     * 给定两个字符串 s1 和 s2，请编写一个程序，确定其中一个字符串的字符重新排列后，能否变成另一个字符串。
     * 输入: s1 = "abc", s2 = "bca"
     * 输出: true
     * 输入: s1 = "abc", s2 = "bad"
     * 输出: false
     * @param s1
     * @param s2
     * @return
     */
    public static boolean CheckPermutation(String s1, String s2) {
        if ((s1==null && s2==null) ||(s1.length()==0 && s2.length()==0)){
            return true;
        }
        if (s1.length()!=s2.length()){
            return false;
        }
        List<Character> list1 = new ArrayList<>();
        List<Character> list2 = new ArrayList<>();
        for (int i = 0; i < s1.length(); i++){
            list1.add(s1.charAt(i));
            list2.add(s2.charAt(i));
        }
        Collections.sort(list1);
        Collections.sort(list2);
        for (int i = 0 ; i< list1.size() ; i++){
            if (!list1.get(i).equals(list2.get(i))){
                return false;
            }
        }
        return true;
    }


    public boolean CheckPermutation2(String s1, String s2) {
        int l1=s1.length(),l2=s2.length();
        if(l1!=l2) {
            return false;
        }
        int []index=new int[128];
        for(int i=0;i<l1;i++){
            index[s1.charAt(i)]++;
            index[s2.charAt(i)]--;
        }
        for(int i=0;i<128;i++){
            if(index[i]!=0) {
                return false;
            }
        }
        return true;
    }

}
